﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using WebPortal.Models;
using System.Web.Routing;
using Model;

namespace WebPortal.WebService
{
    /// <summary>
    /// Wanda_ETL 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [Serializable]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class Wanda_MDP : System.Web.Services.WebService
    { 
        List<WebServiceAudit> list; 
        int threads;
        
        /// <summary>
        /// 根据传入的用户名、视图名+系统配置返回结果集，测试阶段，需要等整体路线贯通后再行更改！
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="vwname">视图名</param>
        /// <returns>结果集</returns>
        [WebMethod]
        public DataTable GetData(string SystemCode, string ViewName, string DBName)
        {
            LoginInfo info = new LoginInfo();
            info.DBName = DBName;
            BLL.GetDataByVW_NameForWebServiceBLL gd = new BLL.GetDataByVW_NameForWebServiceBLL(info);

            if (Application["WebServiceAudit"] == null)
            {
                list = new List<WebServiceAudit>();
                Application.Add("WebServiceAudit", list);
            }
            threads = gd.GetThreads(SystemCode);
            list = (List<WebServiceAudit>)Application["WebServiceAudit"];
            int wsa_count = (from wsa_l in list where wsa_l.UserName == SystemCode && wsa_l.VwName == ViewName select wsa_l).Count();
            if (wsa_count >= threads)
            {
                return null;
            }
            WebServiceAudit wsa = new WebServiceAudit();
            wsa.UserName = SystemCode;
            wsa.VwName = ViewName;
            wsa.Threads = threads;
            list.Add(wsa);
            if (gd.GetViewPushType(SystemCode, SystemCode, ViewName) == 0)
            {
                return null;
            }
            DataTable dt = gd.GetData(SystemCode, SystemCode, ViewName);
            
            list.Remove(wsa);
            return dt;
        }


        

    }
}
